#Persistent
#SingleInstance force
AppName := "Audio Switcher"

gBackgroundTimer := 500
  

menu, tray, NoStandard
menu, tray, icon, audio.ico ,, 1
menu, tray, tip, %AppName%

Menu, DeviceSubMenu, add, Speaker, SetSpeaker
Menu, DeviceSubMenu, add, S/PDIF, SetSpdif
Menu, DeviceSubMenu, add, Toggle, ToggleAudio

Menu, tray, add, Device, :DeviceSubMenu
Menu, tray, add, Exit, ExitSub

SetTitleMatchMode, slow


InitBackground()
return


WaitWinActive(WindowName, Retries)
{
  loop, %Retries%
  {
    WinActivate, %WindowName%
    WinWaitActive, %WindowName%,, 1
    IfWinActive, %WindowName%
    {
      return
    }
  }        
}

;---------------------------------------------------------------------------
;
;   SetAudioDevice
; 
;  
;
SetAudioDevice(Device)
{
  global gCurrentAudioDevice
  ;SplashImage,audio.jpg, b , `nAudio Device: %Device% 

  Run, mmsys.cpl
  WaitWinActive("Sound", 20)
  if (Device == "S/PDIF")
  {
    ; MODIFY HERE: Press 3 times down button to select S/PDIF output in the list view
     ControlSend, SysListView321,{Down}{Down}{Down}
  }

  if (Device == "Speaker")
  {
    ; MODIFY HERE: Press 1 time down button to select speaker output in the list view
    ControlSend, SysListView321,{Down}
  }

  WaitWinActive(Sound, 5)
  ControlClick,Button2
  WaitWinActive(Sound, 5)
  ControlClick, OK
  
  gCurrentAudioDevice := Device
  ;SplashImage, Off   
}

;---------------------------------------------------------------------------
;
;   ToggleAudioDevice
; 
;  
;
ToggleAudioDevice()
{
  global gCurrentAudioDevice
  if (gCurrentAudioDevice == "Speaker")
  {
    setAudioDevice("S/PDIF")
  } else {
    setAudioDevice("Speaker")
  }
}

    


;---------------------------------------------------------------------------
;
;   InitAudioSetting
; 
;  
;
InitAudioSetting()
{
  global gPreviousState
  global gCurrentState
  ifWinExist, Windows Media Center
  {
    SetAudioDevice("S/PDIF")
    currentState := "MceActive"
  } else {
    SetAudioDevice("Speaker")
    currentState := "MceInactive"
  }
  gPreviousState := gCurrentState
}



;---------------------------------------------------------------------------
;
;   CheckAudioSetting
; 
;   procedure to switch to correct audio output
;
CheckAudioSetting()
{
  global gCurrentAudioDevice
  global gPreviousState
  global gCurrentState

  ifWinExist, Windows Media Center
  {
    gCurrentState := "MceActive"
    WinActivate, Windows Media Center
  } else {
    gCurrentState := "MceInactive"
  }

  if (gPreviousState == gCurrentState)
  {
    ;
  } else {
    
    if (gCurrentState == "MceActive")
    {
        SetAudioDevice("S/PDIF")      
    } else {            
      SetAudioDevice("Speaker")
    }
  }
  gPreviousState := gCurrentState   
}


;---------------------------------------------------------------------------
;
;   BackgroundTimer
; 
;   subroutine is called when timer expires
;
BackgroundTimer:
	 BackgroundThread()
return

BackgroundThread()
{
  global gBackgroundTimer
	CheckAudioSetting()
  setTimer, BackgroundTimer, -%gBackgroundTimer%
}

InitBackground()	 
{  
  global gBackgroundTimer
  setTimer, BackgroundTimer, -%gBackgroundTimer%
}


SetSpdif:
  SetAudioDevice("S/PDIF")
Return

SetSpeaker:
  SetAudioDevice("Speaker")
Return

ToggleAudio:
  ToggleAudioDevice()
Return

ExitSub: 
  ExitApp
return


